package engineer.clientapp.actions;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;


import engineer.clientapp.beans.User;
import engineer.clientapp.tools.Connector;



public class EditAccountAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession(true);
		User sessions=(User)session.getAttribute("user");
		String target=null;
		User user=(User)form;
		String name= user.getName();
		String surname=user.getSurname();
		String address=user.getAddress();
		String email=user.getEmail();
		String password= DigestUtils.md5Hex(user.getPassword());
		ActionErrors actionErrors = new ActionErrors();
		
		try{
			Connection connection = new Connector().getConnection();
    		Statement statement = connection.createStatement();
    		statement.setMaxRows(1);
    		ResultSet result = statement.executeQuery("SELECT email, password, name, surname, address id FROM automats.users WHERE id=\""+ sessions.getId() + "\"");
    		if(result.next()){
    			if(!(name.equals("") || surname.equals("") || email.equals("") || address.equals(""))){
    				if(password.equals(sessions.getPassword())){
    					statement.executeUpdate("Update automats.users set name=\""+name+"\", surname=\""+surname+"\", address=\""+address+"\", email=\""+email+"\" WHERE id='"+sessions.getId()+"'");
    					sessions.setName(name);
    					sessions.setSurname(surname);
    					sessions.setAddress(address);
    					sessions.setEmail(email);
    				
    					session.setAttribute("user", sessions);
    					target="success";
    					
    					actionErrors.add("error",
    							new ActionMessage("error.editAccountSuccess"));
    					result.close();
    					connection.close();
    				}else{
    				//bledne hasla
    					actionErrors.add("error",
    							new ActionMessage("error.badPassword"));
    					result.close();
    					connection.close();
    				}
    			}else{
    				//wyjatek pustego pola
    				actionErrors.add("error",
							new ActionMessage("error.emptyFields"));
					result.close();
					connection.close();
    			}
    		}else{
    			//wyjatek resultset
    			actionErrors.add("error",
						new ActionMessage("error.nouser"));
				result.close();
				connection.close();
    		}
		}catch(Exception e){
			actionErrors.add("error",
					new ActionMessage("error.catch"));
		}
		
		if(actionErrors.size() !=0){
			extracted(request, actionErrors);
			return new ActionForward(mapping.getInput());
		}
		return mapping.findForward(target);
	}
	private void extracted(HttpServletRequest request, ActionErrors actionErrors) {
		saveErrors(request, actionErrors);
	}
}